#include "..\\code\\dataStruct.h"
#include "..\\code\\common.h"
#include <math.h>
#include <stdio.h>
extern void gtTrip(gtranDevice* gtrandevice);
extern void gtOver1(gtranDevice* gtrandevice);
extern void gtOver2(gtranDevice* gtrandevice);
extern void gtZero1(gtranDevice* gtrandevice);
extern void gtZero2(gtranDevice* gtrandevice);
extern void gtLow(gtranDevice* gtrandevice);

void gtran(gtranDevice *gtrandevice){

    // 将采样值存入瞬时值数组
    gtranSample2inst(gtrandevice);

    // 瞬时值滤波后存入并更新滤波后数组
    gtranDataFilter(gtrandevice);

    // 利用滤波后数据计算12通道相量,存入phasor数组
    gtranToPhasor(gtrandevice);

    gtranHalfPhasor(gtrandevice);

    gtranHalfInte(gtrandevice);

//    gtrandevice->gtranProRes[1] = phasorAbs(gtrandevice->phasor[0]);
//    gtrandevice->gtranProRes[2] = phasorAbs(gtrandevice->phasor[1]);
//    gtrandevice->gtranProRes[3] = phasorAbs(gtrandevice->phasor[2]);
//    gtrandevice->gtranProRes[4] = phasorAbs(gtrandevice->phasor[3]);
//    gtrandevice->gtranProRes[5] = phasorAbs(gtrandevice->phasor[4]);
//    gtrandevice->gtranProRes[6] = phasorAbs(gtrandevice->phasor[5]);
//    gtrandevice->gtranProRes[7] = gtrandevice->phasor[0].real;
//    gtrandevice->gtranProRes[8] = gtrandevice->phasor[1].real;
//    gtrandevice->gtranProRes[9] = gtrandevice->phasor[2].real;
//    gtrandevice->gtranProRes[10] = phasorAbs(gtrandevice->phasor[6]);
//
    if((gtrandevice->quickCurStartFlag == 0) && (gtrandevice->mainFlag == 0)){
        gtTripStart(gtrandevice);
    }
    if((gtrandevice->quickCurStartFlag == 1) && (gtrandevice->mainFlag == 0) && (gtrandevice->startEnable[0] == 1)){
        gtTrip(gtrandevice);
    }

    if((gtrandevice->overCurStartFlag1 == 0) && (gtrandevice->mainFlag == 0)){
        gtOverStart1(gtrandevice);
    }
    if((gtrandevice->overCurStartFlag1 == 1) && (gtrandevice->mainFlag == 0) && (gtrandevice->startEnable[1] == 1)){
        gtOver1(gtrandevice);
    }

    if((gtrandevice->overCurStartFlag2 == 0) && (gtrandevice->mainFlag == 0)){
        gtOverStart2(gtrandevice);
    }
    if((gtrandevice->overCurStartFlag2 == 1) && (gtrandevice->mainFlag == 0) && (gtrandevice->startEnable[2] == 1)){
        gtOver2(gtrandevice);
    }

    if((gtrandevice->zeroCurStartFlag1 == 0) && (gtrandevice->mainFlag == 0)){
        gtOverStart1(gtrandevice);
    }
    if((gtrandevice->zeroCurStartFlag1 == 1) && (gtrandevice->mainFlag == 0) && (gtrandevice->startEnable[3] == 1)){
        gtOver1(gtrandevice);
    }

    if((gtrandevice->zeroCurStartFlag2 == 0) && (gtrandevice->mainFlag == 0)){
        gtOverStart2(gtrandevice);
    }
    if((gtrandevice->zeroCurStartFlag2 == 1) && (gtrandevice->mainFlag == 0) && (gtrandevice->startEnable[4] == 1)){
        gtOver2(gtrandevice);
    }

    if((gtrandevice->lowCurStartFlag == 0) && (gtrandevice->mainFlag == 0)){
        gtLowStart(gtrandevice);
    }
    if((gtrandevice->lowCurStartFlag == 1) && (gtrandevice->mainFlag == 0) && (gtrandevice->startEnable[5] == 1)){
        gtLow(gtrandevice);
    }

//
    
    if (gtrandevice->time < 0.2) {
        // 等待仿真进入稳定状态
        return;
    }

}
